const app = getApp()
const limits = 15;
import { zg99Request, hbappRequest, jiatuiRequest } from "/utils/requests";
Page({
    data: {
        showContact: true,
        suspendShow: true,
        showIcon: true,
        bgcolor: '',
        showIcon: !0,
        isShowAuth: false,
        myPhone: null,
        years: ['请选择', '2018', '2019', '2020', '2021', '2022'],
        months: ['请选择', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
        areas: [],
        hbAreasArr: [{
            "name": "河北",
            "code": "hb"
        }, {
            "name": "石家庄",
            "code": "sjz"
        }, {
            "name": "邢台",
            "code": "xt"
        }, {
            "name": "邯郸",
            "code": "hd"
        }, {
            "name": "衡水",
            "code": "hs"
        }, {
            "name": "保定",
            "code": "bd"
        }, {
            "name": "承德",
            "code": "cd"
        }, {
            "name": "沧州",
            "code": "cz"
        }, {
            "name": "唐山",
            "code": "ts"
        }, {
            "name": "廊坊",
            "code": "lf"
        }, {
            "name": "张家口",
            "code": "zjk"
        }, {
            "name": "秦皇岛",
            "code": "qhd"
        }, {
            "name": "雄安新区",
            "code": "xaxq"
        }],
        tjAreasArr: [{
            "name": "天津",
            "code": "tj"
        }],
        xiangmuArr: [{
            "name": "请选择",
            "code": "",
            "url": ""
        }, {
            "name": "国家公务员",
            "code": "gjgwy",
            "url": "https://hb.offcn.com/html/guojiagongwuyuan/"
        }, {
            "name": "河北公务员",
            "code": "hbgwy",
            "url": "https://hb.offcn.com/html/hebeigongwuyuan/"
        }, {
            "name": "选调生",
            "code": "xds",
            "url": "https://hb.offcn.com/html/xuandiaosheng/"
        }, {
            "name": "事业单位",
            "code": "sydw",
            "url": "https://hb.offcn.com/html/shiyedanwei/"
        }, {
            "name": "教师招聘",
            "code": "jszp",
            "url": "https://hb.offcn.com/html/jiaoshi/"
        }, {
            "name": "特岗教师",
            "code": "tgjs",
            "url": "https://hb.offcn.com/html/jiaoshi/"
        }, {
            "name": "教师资格",
            "code": "jszg",
            "url": "https://hb.offcn.com/html/jiaozi/"
        }, {
            "name": "医疗卫生",
            "code": "ylws",
            "url": "https://hb.offcn.com/html/yiliaoweisheng/"
        }, {
            "name": "金融银行",
            "code": "jryh",
            "url": "https://hb.offcn.com/html/yinhangkaoshi/"
        }, {
            "name": "农信社",
            "code": "nxs",
            "url": "https://hb.offcn.com/html/nongxinshe/"
        }, {
            "name": "国企",
            "code": "gq",
            "url": "https://hb.offcn.com/html/gqzp/"
        }, {
            "name": "电力电网",
            "code": "gjdw",
            id: 66,
            "url": "https://hb.offcn.com/html/gqzp/"
        }, {
            "name": "烟草",
            "code": "yc",
            id: 67,
            "url": "https://hb.offcn.com/html/gqzp/"
        }, {
            "name": "铁路局",
            "code": "tlj",
            id: 68,
            "url": "https://hb.offcn.com/html/gqzp/"
        }, {
            "name": "三大运营商",
            "code": "sdyys",
            id: 69,
            "url": "https://hb.offcn.com/html/gqzp/"
        }, {
            "name": "石油石化",
            "code": "sty",
            id: 70,
            "url": "https://hb.offcn.com/html/gqzp/"
        }, {
            "name": "公选/遴选",
            "code": "gxlx",
            "url": "https://hb.offcn.com/html/gongxuan/"
        }, {
            "name": "三支一扶",
            "code": "szyf",
            "url": "https://hb.offcn.com/html/sanzhiyifu/"
        }, {
            "name": "书记员",
            "code": "sjy",
            "url": ""
        }, {
            "name": "社区工作者",
            "code": "sqgzz",
            "url": "https://hb.offcn.com/html/shequgongzuozhe/"
        }, {
            "name": "公益岗",
            "code": "gyg",
            "url": ""
        }, {
            "name": "学历考试",
            "code": "xlks",
            "url": "https://hb.offcn.com/html/zikaochengkao/"
        }, {
            "name": "考研",
            "code": "ky",
            "url": "https://hb.offcn.com/html/zzss/"
        }, {
            "name": "专接本",
            "code": "zjb",
            "url": "https://www.kaozhiye.com/?wt.bd=zg58"
        }, {
            "name": "会计",
            "code": "kj",
            "url": "https://hb.offcn.com/html/kuaijiquzheng/"
        }, {
            "name": "法考",
            "code": "fk",
            "url": "https://hb.offcn.com/html/fakao/"
        }, {
            "name": "招警",
            "code": "zj",
            "url": "https://hb.offcn.com/html/zhaojing/"
        }, {
            "name": "公安文职",
            "code": "gawz",
            "url": ""
        }, {
            "name": "辅警",
            "code": "fj",
            "url": ""
        }, {
            "name": "军转干",
            "code": "zjg",
            "url": "https://hb.offcn.com/html/junzhuangan"
        }, {
            "name": "护师",
            "code": "hushi",
            "url": ""
        }, {
            "name": "药师",
            "code": "yaoshi",
            "url": ""
        }, {
            "name": "军队文职",
            "code": "jdwz",
            "url": ""
        }, {
            "name": "建工",
            "code": "jiangong",
            "url": ""
        }, {
            "name": "成人高考",
            "code": "crgk",
            "url": ""
        }, {
            "name": "消防",
            "code": "xf",
            "url": "https://xiaofang.offcn.com/html/hb/?channel=hb"
        }],
        index_xiangmu: 2,
        index_area: 0,
        index_year: 0,
        index_month: 0,
        nodata: false,
        lists: [],
        page: 1,
    },
    async onLoad(options){
        this.setData(await app.methods.myPhone()); // 获取本地存储手机号
        if(!this.data.myPhone) {
            await app.methods.getPhoneNumber()
            this.setData({
              isShowAuth: !0
            })
        }
        xhs.showLoading({
            title: '数据加载中....',
            mask: true
        })
        xhs.showShareMenu({
          shareTypes: 'normal',
        })
        let now = new Date()
        let years = now.getFullYear();
        let month = now.getMonth() + 1 < 10 ? `0${now.getMonth() + 1}` : now.getMonth() + 1;
        let day = now.getDate() < 10 ? `0${now.getDate()}` : now.getDate();
        this.setData({
            today: `${years}-${month}-${day}`
        })
        let links, jxOptions;
        if (options.scene) {
            options.scene = options.scene.replace(/%253D/g, ".");
            options.scene = options.scene.replace(/%2526/g, "-");
            jxOptions = await app.methods.getOPtions(options);
        }
        let id = options.scene ? jxOptions.id : options.id;
        options.id = id;
        links = await app.methods.getLink(id);
        options.area = links.area ? links.area : '';
        options.xiangmu = links.xiangmu ? links.xiangmu : '';
        options.actid = links.actid ? links.actid : '';
        options.fx = options.scene ? jxOptions.fx : options.fx;
        options.gh = options.scene ? jxOptions.gh : options.gh;
        this.setData({
            options,
        })
        //查询options参数对应的项目和地区;
        var flag1 = {
            code: options.area
        };
        var flag2 = {
            code: options.xiangmu
        };
        if (options.area == "") {
            this.setData({
                index_area: 0,
                area: '河北',
                areas: this.data.hbAreasArr
            })
        } else if(options.area == "tj") {
            let { xiangmuArr } = this.data;
            xiangmuArr[2] = {
                "name": "天津公务员",
                "code": "tjgwy",
                "url": "https://hb.offcn.com/html/hebeigongwuyuan/"
            }
            this.setData({
                index_area: 20,
                area: '天津',
                areas: this.data.tjAreasArr,
                xiangmuArr
            })
        } else {
            this.setData({
                areas: this.data.hbAreasArr
            })
            var index_area = (this.data.areas || []).findIndex((profile) => profile.code === flag1.code);
            this.setData({
                index_area,
                area: this.data.areas[index_area].name,
            })
        }
        if (options.xiangmu == "") {
            this.setData({
                index_xiangmu: 0,
                xiangmu: '',
                xiangmu_flag: false,
                month: options.month,
                year: options.year
            })
        } else {
            var index_xiangmu = (this.data.xiangmuArr || []).findIndex((profile) => profile.code === flag2.code);
            this.setData({
                index_xiangmu: index_xiangmu,
                xiangmu_flag: true,
                xiangmu: this.data.xiangmuArr[index_xiangmu].name,
                catid2: this.data.xiangmuArr[index_xiangmu].id ? this.data.xiangmuArr[index_xiangmu].id : undefined
            })
        }
        await this.getYears()
        this.setData(await app.methods.getIdentity(options)); // 获取个人身份信息
        if(this.data.getIdentity.crm) {
            options.wxh = this.data.getIdentity.crm
        } else {
            options.wxh = options.gh
        }
        this.setData(await app.methods.getWechat(options)); // 获取个人身份信息
        this.setData(await app.methods.guishudi(options)); // 获取归属地
        let fenbu = this.data.fenbu;
        let fenxiao = this.data.fenxiao;
        let getIdentity = this.data.getIdentity;
        getIdentity.fenbu = fenbu;
        getIdentity.fenxiao = fenxiao;
        getIdentity.zzname = this.data.zzname;
        getIdentity.zzid = this.data.zzid;
        getIdentity.erpdqm = this.data.erpdqm;
        this.setData({
            getIdentity: getIdentity,
            laiyuan: this.data.area + this.data.xiangmu + '招考信息汇总',
            url1: `/packageB/acsummary/acsummary?id=9286&gh=${options.gh}&fx=${options.fx}&actid=${this.data.options.actid}`,
            url2: `/packageB/bargain/index?id=54276&gh=${options.gh}&fx=${options.fx}`,
        })
        app.globalData.laiyuan = `${this.data.area}${this.data.xiangmu}招考信息汇总`
        console.log(this.data)
        if(this.data.myPhone) {
            this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.options.actid, 'chaxun'));
            await this.thisMethod();
        }
        await this.search();
        xhs.hideLoading();
        console.log(this.data)
    },
    // 获取年份信息（系统中累计有多少年的公告）
    async getYears() {
        return new Promise((resolve, reject) => {
            hbappRequest({
                url: '/hbqt/web/index.php/Gghz/index/index_ggxxhz_year?callback=&',
                method: 'GET',
                dataType: 'jsonp',
                data: '',
            }).then(res => {
                let data_years = app.methods.getAPIReturn(res);
                // console.log(data_years)
                data_years.data.push({
                    year: '请选择年份'
                })
                this.setData({
                    data_years: data_years.data
                })
                resolve()
            }).catch(err => {
                console.log(err)
                resolve()
            })
        })
    },
    // 选择年份/月份/地市/项目
    bindPickerChange: function (e) {
        const { method } = e.currentTarget.dataset;
        if (app.methods.bindPickerChange(e) != 0) {
            let newArr = method == 'year' ? this.data.data_years : method == 'area' ? this.data.areas : method == 'month' ? this.data.months : this.data.xiangmuArr;
            this.setData({
                [method]: method == 'month' ? newArr[Number(app.methods.bindPickerChange(e)[method])] : newArr[Number(app.methods.bindPickerChange(e)[method])][method == 'area' || method == 'xiangmu' ? 'name' : method],
                ['index_'+method]: Number(app.methods.bindPickerChange(e)[method])
            })
        } else {
            this.setData({
                [method]: '',
                ['index_'+method]: Number(app.methods.bindPickerChange(e)[method])
            })
        }
        this.setData({
            lists: [],
            page: 1
        })
        this.search()
    },
    // 查询数据操作
    async search() {
        if (this.data.year == "" || this.data.year == undefined || this.data.year == '请选择年份') {
            this.setData({
                year: ''
            })
        }
        if (this.data.month == "" || this.data.month == undefined || this.data.month == '请选择') {
            this.setData({
                month: ''
            })
        }
        this.setData({
            lists: [],
            page: 1
        })
        await this.getLists(1)
    },

    async thisMethod() {
        if (!this.data.user) {
            await app.methods.registerCX(this.data.myPhone, this.data.options.actid, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, '', '', this.data.book, 'chaxun')
            this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.options.actid, 'chaxun'))
        } else {
            await app.methods.updateuserCX(this.data.myPhone, this.data.options.actid, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, this.data.user.user.gwlb, '', 'chaxun', this.data.book, this.data.user.user)
            this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.options.actid, 'chaxun'))
        }
    },

    // 走查询公告列表接口
    async getGgList(data) {
        return new Promise((resolve) => {
            hbappRequest({
                url: '/hbqt/web/index.php/Gghz/index/index_gghz_pages_list?callback=&',
                method: 'GET',
                dataType: 'jsonp',
                data: {
                    ...data,
                    time: Date.parse(new Date())
                },
            }).then(res => {
                let resdata = app.methods.getAPIReturn(res);
                let list = this.data.lists
                this.setData({
                    lists: list.concat(resdata.data.list),
                })
                if (resdata.data.list.length == 0 && this.data.page == 1) {
                    this.setData({
                        nodata: true
                    })
                } else {
                    this.setData({
                        nodata: false
                    })
                }
                resolve()
            }).catch(err => {
                console.log(err)
                resolve()
            })
        })
    },

    // 打开公告
    gotoArticle: function (e) {
        let { url, id } = e.currentTarget.dataset;
        console.log(this.data.myPhone)
        if ( !this.data.myPhone) {
            this.setData({
                isShowAuth: true
            })
        } else {
            xhs.navigateTo({
                url: `/packageB/gg/gg?link=${url}&id=${id}`,
            })
        }
    },

    async user_login(e){
        if(typeof e.detail == 'boolean') {
            this.setData({
                isShowAuth: e.detail
            })
        } else {
            this.setData({
                myPhone: e.detail
            })
            this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.options.actid, 'chaxun'));
            await this.thisMethod();
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: async function () {
        xhs.stopPullDownRefresh();
        if(this.data.page == this.data.allPage) {
          xhs.showToast({
            title: '我是有底线嗒~',
          })
          return false
        }
        this.setData({
            page: ++this.data.page
        })
        await this.getLists(this.data.page)
    },
    
    // 回到顶部
    goTop: function () {
        xhs.pageScrollTo({
            scrollTop: 0,
            duration: 800
        })
    },

    onShareAppMessage(){
        let { area, xiangmu } = this.data;
        let { fx = 'hbwtb', gh = 'zcj37368', id } = this.data.options;
        console.log(`${area}${xiangmu}近期公告汇总`)
        console.log(`/packageB/ggsummary/index?id=${id}&fx=${fx}&gh=${gh}`)
        return {
            title: `${area}${xiangmu}近期公告汇总`,
            path: `/packageB/ggsummary/index?id=${id}&fx=${fx}&gh=${gh}`,
            imageUrl: '',
        }
    },

    // 获取输入框内容
    inputInfor(e){
      if (e.currentTarget.dataset.method == 'notice_name') {
        this.setData({
          notice_name: e.detail.value
        })
      }
    },

    // 查询按钮
    async searchNotice(){
      this.setData({
        page: 1,
        lists: []
      })
      await this.getLists(1)
    },
    
    // 查询数据接口
    getLists(page){
      const { notice_name, xiangmu, catid2, area, year, month } = this.data;
      let searchData = {
        notice_name: notice_name ? notice_name : '',
        cate_name: catid2 ? '' : xiangmu == '请选择' ? '' : xiangmu,
        catid2: catid2 ? catid2 : '',
        areaname2: area == '天津' ? null : notice_name ? '' : '河北',
        // areaname2: area == '河北' ? notice_name ? '' : area : area == '天津' ? null : '河北',
        areaname: area == '河北' ? notice_name ? '' : '' : area,
        // areaname2: area == '河北' ? area : area == '天津' ? null : '河北',
        // areaname: area == '河北' ? '' : area ,
        year,
        month,
        limits,
        page,
      }
      return new Promise(resolve => {
        jiatuiRequest({
          url: `/api/v1/moonshot/getNoticeListForMiniProgram`,
          type: 'GET',
          dataType: 'jsonp',
          header: {
            'X-Requested-From': 'WeChatMiniProgram'
          },
          data: searchData
        }).then(res => {
            let { code, data } = res;
            const { lists } = this.data;
            if(code == 0) {
                this.setData({
                lists: [...lists, ...data.list],
                total: data.total,
                allPage: Math.ceil(data.total/limits)
                })
            } else {
                xhs.showToast({
                title: '查询异常，稍后再试',
                icon: 'error'
                })
            }
            resolve()
        }).catch(err => {
            xhs.showToast({
                title: '查询异常，稍后再试',
                icon: 'error'
            })
            resolve()
        })
      })
    },
})